

cc.Class({
    extends: cc.Component,

    properties: {
     
    },

    onLoad(){
        this.init()
        this.toLeft = true
    },

    init(speed){
        this.hp = 25
        this.num = this.hp
        this.goldCount = 5
        this.isDie = false
        var anim = this.getComponent(cc.Animation)
        anim.play('enemy2_Normal')

        this.beGold = game.playing.getBeGold()
        this.beGoldPos = game.playing.getBeGoldWorldPos()

        this.speed = 80 + speed
    },

    normal(){
        var anim = this.getComponent(cc.Animation)
        anim.play('enemy2_Normal')
    },

    hit(harm){
        this.num -= harm
        if(this.num <= 0) {
            this.die()
        }
        
        //var anim = this.getComponent(cc.Animation)
        // anim.play('enemy2_Hit')
        // anim.over = function(){
        //     this.normal()
        // }.bind(this)
    },

    die(){
        this.isDie = true
        game.playing.changeProgress(this.hp)
        var anim = this.getComponent(cc.Animation)
        anim.play('enemy2_Die')
        game.playEnemyDieSound()
        utils.shakeEffect(game.bg_1);

        anim.over = function(){
            game.delPrefab(this.node, 2)
            game.addProp(this.node)
            game.createGoldAnim(
                this.node.getPosition(),
                this.beGoldPos,
                this.beGold,
                100,
                this.goldCount,
                1,
                (gold) => {
                  game.playing.getGold(50)
                }
            );
        }.bind(this)

    },

   
    update (dt) {
        if(this.isDie) return
        if(game.gameStatus != 1) return
        var speed = this.speed * dt
        if(!game.isAttack) speed = 30 * dt
        this.node.y = this.node.y - speed
        if(this.toLeft){
            this.node.x = this.node.x - speed
            if( this.node.x < -270) this.toLeft = false
        }
        if(!this.toLeft){
            this.node.x = this.node.x + speed
            if( this.node.x > 270) this.toLeft = true
        }

        if(this.node.y <= -650){
            game.delPrefab(this.node, 2)
        }
    },
});
